import {
	useGetEventType,
	useGetEventCategory
} from "../hooks/useEventCategory"
import {
	ClassTypeEnum
} from "@/enums/event"
import {
	formKeys
} from "../report.form.config"
import {
	getTylc
} from "../../../../api/Interface/event"

export default {
	data() {
		return {
			eventCascaderShow: false,
			meCheckPath: "",
			defaultFlow: null
		}
	},
	onLoad() {

	},
	onReady() {
		this.getDefaultFlowId();
		this.$nextTick(() => {
			this.meInitEventCategory()
		})
	},
	methods: {
		// 获取默认统一流程信息
		getDefaultFlowId() {
			getTylc().then(res => {
				if (res.code === 200) this.defaultFlow = res.data?.[0];
			})
		},
		// 开启组件
		meCheckEventCategory() {
			this.eventCascaderShow = true;
		},
		async meInitEventCategory() {
			const {
				data
			} = await useGetEventType()
			console.log("meInit", data, this.$refs.RefCascader);
			if (data) {
				// let list = [...data, ...data, ...data, ...data, ...data, ...data, ...data, ...data, ...data,...data, ...data, ...data, ...data, ...data, ...data, ...data, ...data, ...data]
				this.$refs.RefCascader.push(data);
			}
		},
		/**
		 * @description 事件类型级联组件点选回调
		 * @param {Number} level 当前选择的层级
		 * @param {Object} e 当前选择的数据对象
		 * 
		 */
		async meCheckEventConfirm({
			level,
			e
		}) {
			let list = []
			if (level === 0) {
				const params = {
					// flowType: 1,
					eventTypeItem: e.dictValue === "1" ? 0 : 1,
					eventTypePid: ClassTypeEnum.big
				}
				const {
					data
				} = await useGetEventCategory(params)
				if (data) list = data;
			} else if (level >= 1) {
				const params = {
					eventTypePid: e.eventTypeId
				}
				const {
					data
				} = await useGetEventCategory(params)
				if (data) list = data;
			}
			this.$refs.RefCascader.push(list, this.meVerify);
		},
		meVerify(level, array) {
			if (level === 0 && (!array || array.length === 0)) {
				this.$toast("未查询到节点下的大类信息!")
				return false;
			}
			return true
		},
		meChooseConfirm({
			chooseItem,
			choosePath,
			path,
			level
		}) {
			if (level === 0) {
				this.$toast("未查询到节点下的大类信息!")
				return;
			}
			// if (uni.$u.test.empty(chooseItem.currentTaskId)) {
			// 	this.$toast("当前事件类型未配置流程,无法使用!")
			// 	return;
			// }
			if (chooseItem.eventTypeId?.toString() !== this.form[formKeys.eventTypeId]) this.meCheckLinkage();
			const flows = choosePath.filter(m => m.flow);
			let flowName = "";
			let taskId = "";
			if (flows.length > 0) {
				flowName = flows[flows.length - 1].flow;
				taskId = flows[flows.length - 1].taskId;
			} else {
				flowName = this.defaultFlow?.id;
				taskId = this.defaultFlow?.taskId;
			}
			console.log("flows", flows);
			this.taskId = taskId; // chooseItem.currentTaskId; // 下一节点处理人前置处理
			this.form[formKeys.eventTypeId] = chooseItem.eventTypeId?.toString();
			if (chooseItem.eventTypePid === ClassTypeEnum.big) {
				this.form[formKeys.eventCategory] = chooseItem.eventTypeName;
				this.form[formKeys.flowName] = flowName;
				this.form[formKeys.eventSubclass] = null;
			} else if (![...Object.values(ClassTypeEnum)].includes(chooseItem.eventTypePid)) {
				let index = choosePath.findIndex(f => f.value === chooseItem.eventTypeId);
				if (index >= 1) this.form[formKeys.eventCategory] = choosePath[index - 1].label;
				this.form[formKeys.eventSubclass] = chooseItem.eventTypeName;
				flowName && (this.form[formKeys.flowName] = flowName)
			}
			this.meCheckPath = path; // choosePath.map(m => m.label).join("/")
			console.log("mechooseconfirm", chooseItem, choosePath, JSON.parse(JSON.stringify(this.form)));
		},
		// meChooseConfirm选择后与"下一节点处理人"组件的联动
		meCheckLinkage() {
			this.form[formKeys.eventType] = null;
			this.form[formKeys.eventHandler] = null;
			this.parents = null;
			this.mhCheckPath = null;
			this.$refs.RefHandleCascader.reset();
		}
	}
}